package ar.fi.uba.fallas2.business;

import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;

import ar.fi.uba.fallas2.data.DataException;
import ar.fi.uba.fallas2.data.Database;

public class AnswerKind extends BusinessObject {
	private String value;
	private static final String insertStatementString = "INSERT INTO answer_kind(value) VALUES (?)";
	private static final String updateStatementString = "UPDATE answer_kind SET value = ? WHERE id_kind = ?";
	private static final String deleteStatementString = "DELETE FROM answer_kind WHERE id_kind = ?";
	
	public static final AnswerKind YES 		= new AnswerKind(1);
	public static final AnswerKind NO 		= new AnswerKind(2);
	public static final AnswerKind PROBABLY = new AnswerKind(3);
	public static final AnswerKind DOUBTFUL = new AnswerKind(4);
	
	private static final Map answers = new HashMap();
	static {
		answers.put("si", AnswerKind.YES);
		answers.put("no", AnswerKind.NO);
		answers.put("probablemente", AnswerKind.PROBABLY);
		answers.put("dificilmente", AnswerKind.DOUBTFUL);
		answers.put("irrelevante", null);
	}
	
	public AnswerKind() {
		super("answer_kind");
	}
	
	public AnswerKind(int id) {
		super("answer_kind", id);
	}
	
	protected void insert() throws DataException {
		PreparedStatement insertStatement = null;
		try {
			insertStatement = Database.getInstance().prepareStatement(insertStatementString);
			insertStatement.setString(1, this.getValue());
			insertStatement.execute();
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	protected void update() throws DataException {
		PreparedStatement updateStatement = null;
		try {
			updateStatement = Database.getInstance().prepareStatement(updateStatementString);
			updateStatement.setString(1, this.getValue());
			updateStatement.setInt(2, this.getId());
			updateStatement.execute();
		} catch (Exception e) {
			throw new DataException(e);
		}
	}
	
	public void delete() throws DataException {
		PreparedStatement updateStatement = null;
		try {
			updateStatement = Database.getInstance().prepareStatement(deleteStatementString);
			updateStatement.setInt(1, this.getId());
			updateStatement.execute();
		} catch (Exception e) {
			throw new DataException(e);
		}
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getValue() {
		return value;
	}

	public static AnswerKind getAnswer(String answer) {
		return (AnswerKind)answers.get(answer);
		//return AnswerKind.getAnswer(answer);
	}
	
	public boolean equals(Object o){
		AnswerKind other = (AnswerKind)o;
		return this.getId() == other.getId();
	}
}
